package edu.hawaii.myisern.action;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import com.meterware.httpunit.TableCell;
import edu.hawaii.myisern.model.Log;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Tests operation of the MyIsern researcher web pages.
 * 
 * @author Randy Cox
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class TestResearcherActionBeans {

  /** Get the test host. */
  private String Url = System.getProperty("test.host") + "myisern-tan";

  /** User name to use. */
  private String username = "test";
  private String password = "test";
  private String success = "You have logged in successfully!";

  /**
   * Test the main menu new researcher button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testNewResearcherButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern 2.0", response.getTitle());

    // Push login button
    WebForm form = response.getFormWithID("LoginForm");
    WebRequest request = form.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    response = conversation.getResponse(request);
    String text = response.getText();
    assertTrue("Checking login", text.contains(success));

    // Click on select researcher link.
    WebLink link = response.getLinkWithID("SelectResearcherLink");
    response = conversation.getResponse(link.getRequest());
    text = response.getText();
    assertTrue("Checking select researcher", text.contains("List of ISERN Researchers:"));

    // Clear researchers.
    link = response.getLinkWithID("ClearResearcherLink");
    response = conversation.getResponse(link.getRequest());
    WebTable table = response.getTableWithID("SelectResearcherTable");
    Integer expected = 1;
    Integer count = table.getRowCount();
    assertEquals("Researchers should have only header row", expected, count);

    // Click on new researcher link.
    link = response.getLinkWithID("NewResearcherLink");
    response = conversation.getResponse(link.getRequest());
    text = response.getText();
    assertTrue("Checking new researcher", text.contains("Edit ISERN Researcher:"));

    // Click on save.
    form = response.getFormWithID("EditResearcherForm");
    form.setParameter("name", "Test Researcher");
    form.setParameter("email", "Test Email");
    // form.setParameter("organization", "Test Organization");
    form.setParameter("pictureLink", "Test Picture Link");
    form.setParameter("bioStatement", "Test Bio Statement");
    response = conversation.getResponse(form.getRequest("save"));
    text = response.getText();
    assertTrue("Checking return to select researcher", text.contains("List of ISERN Researchers:"));

    // Check if new name is in select list and click it.
    table = response.getTableWithID("SelectResearcherTable");
    TableCell cell = table.getTableCell(1, 1);
    assertNotNull("Check if cell exisits", cell);

    String contents = cell.getText();
    Log.enter("TestReasercherActionBeans cell contents = " + contents);
    link = cell.getLinkWith("Test");
    assertNotNull("Check if link in cell exisits", link);

    // Check that we are back at Edit screen, and check the fields for
    // data.
    response = conversation.getResponse(link.getRequest());
    form = response.getFormWithID("EditResearcherForm");
    assertEquals("Check field.", "Test Researcher", form.getParameterValue("name"));
    assertEquals("Check field.", "Test Email", form.getParameterValue("email"));
    // assertEquals("Check field.", "University of Hawaii", form
    // .getParameterValue("organization"));
    assertEquals("Check field.", "Test Picture Link", form.getParameterValue("pictureLink"));
    assertEquals("Check field.", "Test Bio Statement", form.getParameterValue("bioStatement"));

    // Cancel
    Log.enter("Click on cancel.");
    form = response.getFormWithID("EditResearcherForm");
    response = conversation.getResponse(form.getRequest("cancel"));

  }
}
